package com.letoo.dragon;

import java.util.Arrays;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.letoo.dragon.config.JettyWebServerConfig;
import com.letoo.dragon.config.MyBatisConfig;
import com.letoo.dragon.config.TaskExecutorConfig;
import com.letoo.dragon.config.WebMvcConfig;

/**
 * spring boot启动类
 * 
 * @author dengjie
 *
 */
@Configuration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
@ComponentScan
@Import(value = { MyBatisConfig.class, TaskExecutorConfig.class, JettyWebServerConfig.class, WebMvcConfig.class })
//@EnableDiscoveryClient
public class LetooBootApplication {

	private static final Logger LOG = LoggerFactory.getLogger(LetooBootApplication.class);

	@Bean
	protected ServletContextListener listener() {
		return new ServletContextListener() {

			@Override
			public void contextInitialized(ServletContextEvent sce) {
				LOG.info("ServletContext initialized");
			}

			@Override
			public void contextDestroyed(ServletContextEvent sce) {
				LOG.info("ServletContext destroyed");
			}
		};
	}

	/**
	 * spring boot CommandLineRunner
	 * 
	 * @param ctx
	 *            容器上下文
	 * @return CommandLineRunner
	 */
	@Bean
	public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
		return args -> {
			LOG.info("Let's inspect the beans provided by Spring Boot:");
			String[] beanNames = ctx.getBeanDefinitionNames();
			Arrays.sort(beanNames);
			for (String beanName : beanNames) {
				LOG.info(beanName);
			}

		};
	}

	/**
	 * spring boot主方法入口,启动服务
	 * 
	 * @param args
	 *            参数
	 */
	public static void main(String[] args) {
		LOG.info("通过SpringBoot来注入依赖:");
		SpringApplication.run(LetooBootApplication.class, args);
	}

}
